
package uk.addie.mp3fixer;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.Action;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import uk.addie.mp3fixer.visitors.ChangeAlbum;
import uk.addie.mp3fixer.visitors.ChangeArtist;
import uk.addie.mp3fixer.visitors.DeleteDiscNo;
import uk.addie.mp3fixer.visitors.MakeCompilation;
import uk.addie.mp3fixer.visitors.MoveAllToId3Folder;
import uk.addie.mp3fixer.visitors.RenameLikeId3;
import uk.addie.mp3fixer.widget.ChangeRoot;
import uk.addie.mp3fixer.widget.FileSelectorWidget;
import uk.addie.mp3fixer.widget.GBCChain;
import uk.addie.mp3fixer.widget.MP3InfoWidget;
import uk.addie.mp3fixer.widget.RefreshButton;
import uk.addie.mp3fixer.widget.Scope;
import uk.addie.mp3fixer.widget.ScopeSelectionCombo;

public class MP3Fixer implements Runnable {
    public final static Logger         LOG            = Logger.getLogger(MP3Fixer.class.getSimpleName());
    private static Path                sRoot          = Paths.get(System.getProperty("user.home")).resolve("Music");
    private static FileSelectorWidget  mFileSelector;
    private static MP3InfoWidget       mp3info;
    private static ScopeSelectionCombo ssc;
    private static Action[][]          sActionButtons = {
            { new ChangeAlbum(), new DeleteDiscNo(), new MakeCompilation() }, { new ChangeArtist() },
            { new RenameLikeId3(), new MoveAllToId3Folder() }, { new RefreshButton(), new ChangeRoot() } };

    private static JPanel createControlButtons() {
        final JPanel buttons = new JPanel();
        buttons.setLayout(new GridBagLayout());
        final GridBagConstraints gbc = new GBCChain().gridX(0).gridY(0).weightX(0.25)
                .fill(GridBagConstraints.HORIZONTAL);
        ssc = new ScopeSelectionCombo();
        buttons.add(ssc, gbc);
        gbc.gridx++;
        for (final Action[] category : sActionButtons) {
            gbc.gridy = 0;
            gbc.gridx++;
            for (final Action ab : category) {
                buttons.add(new JButton(ab), gbc);
                gbc.gridy++;
            }
        }
        return buttons;
    }

    public static FileSelectorWidget fileSelector() {
        return mFileSelector;
    }

    public static Path getRoot() {
        return sRoot;
    }

    public static Path getScope() {
        final int selection = ssc.getSelectedIndex();
        final Scope scope = Scope.values()[selection];
        return scope.pathForScope();
    }

    public static void main(final String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException
                | UnsupportedLookAndFeelException e) {
            LOG.log(Level.WARNING, "Can't get platform look-and-feel", e);
        }
        final MP3Fixer se = new MP3Fixer();
        SwingUtilities.invokeLater(se);
        LOG.log(Level.INFO, "MP3Fixer started");
    }

    public static MP3InfoWidget mp3info() {
        return mp3info;
    }

    public static void setRoot(final Path root) {
        sRoot = root;
        mFileSelector.refresh();
    }

    @Override public void run() {
        final JFrame f = new JFrame("Addie's MP3 Fixer");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        final Container outer = f.getContentPane();
        outer.setLayout(new BorderLayout());
        mp3info = new MP3InfoWidget();
        outer.add(mp3info, BorderLayout.LINE_END);
        mFileSelector = new FileSelectorWidget(mp3info);
        outer.add(mFileSelector, BorderLayout.CENTER);
        outer.add(createControlButtons(), BorderLayout.PAGE_END);
        f.pack();
        f.setVisible(true);
    }
}
